<?php
require_once("BaseDAO.php");
require_once("../Entities/Feedback.php");

class FeedbackDAO extends BaseDAO
{
	function CreateFeedback($item)
	{
		$sql = "insert into Feedbacks(QuestionId ,CreatedBy, CreationOn, Description, IsFixed) values ('" . 
			$item->Question . "','" . $item->CreatedBy . "',NOW(),'" . $item->Description . "','" . $item->IsFixed . "');";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		return $item;
	}
	
	function UpdateStatus($id, $isFixed)
	{
		$sql = "update Feedbacks set IsFixed='" . $isFixed . "' where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function DeleteFeedback($id)
	{
		$sql = "delete from Feedbacks where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetFeedbacks($page, $pageSize, &$totalPage, $status)
	{
		if($status == -1)
		{
			$sqlTotalPage = "select * from Feedbacks;";
			$sql = "select * from Feedbacks limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		}
		else
		{
			$sqlTotalPage = "select * from Feedbacks where IsFixed='" . $status . "';";
			$sql = "select * from Feedbacks where IsFixed='" . $status ."' limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		}
		
		$resultTotalPage = mysql_query($sqlTotalPage);
		$result = mysql_query($sql);
		$totalPage = ceil(mysql_num_rows($resultTotalPage) / $pageSize);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Feedback();
			$item->Id = $row["Id"];
			$item->Question = $row["QuestionId"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$item->Description = $row["Description"];
			$item->IsFixed = $row["IsFixed"];
			$list[] = $item;
		}
		return $list;
	}
}

?>